package manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manage.dao.FileMapper;
import manage.dao.FileTypeMapper;
import manage.model.File;
import manage.model.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileServiceImpl {
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private FileTypeMapper fileTypeMapper;

    public PageInfo<File> selectAll(String page, String limit) {

        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);

        PageHelper.startPage(pu,li);
        List<File> list = fileMapper.selectAll();
        list=this.getList(list);
        PageInfo<File> pageInfo = new PageInfo<File>(list);
        return pageInfo;
    }

    public List<File> getList(List<File> list){
        for (File file : list) {
            FileType fileType=fileTypeMapper.selectByPrimaryKey(file.getTypeId());
            file.setType(fileType.getFileName());
        }
        return list;
    }


    public String insert(File record) {
        int i = fileMapper.insert(record);
        if(i>0)
            return "success";
        return "fail";
    }

    public String del(Integer id) {
        int i = fileMapper.deleteByPrimaryKey(id);
        if(i>0)
            return "success";
        return "fail";
    }

    public File select(Integer id) {
        return fileMapper.selectByPrimaryKey(id);
    }
    public String update(File record){
        if(fileMapper.updateByPrimaryKey(record)>0)
            return "success";
        return "fail";
    }

    public PageInfo<File> selectAll1(String page, String limit) {

        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);

        PageHelper.startPage(pu,li);
        List<File> list = fileMapper.selectAll1();
        list=this.getList(list);
        PageInfo<File> pageInfo = new PageInfo<File>(list);
        return pageInfo;
    }
}
